package com.zxit.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxit.entity.*;
import com.zxit.mapper.EmpExprMapper;
import com.zxit.mapper.EmpMapper;
import com.zxit.service.EmpLogService;
import com.zxit.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

//    @Autowired
//    EmpMapper empMapper;
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        //总记录数
//        Long total = empMapper.count();
//        page=(page-1)*pageSize;
//        //列表集合
//        List<Emp> list = empMapper.list(page, pageSize);
//        return new PageResult<Emp>(total, list);
//    }


    /**
     * 基于pageHELPER分页查询
     * @param page
     * @param pageSize
     * @return
     */
//    @Override
//    public PageResult<Emp> page(EmpQueryParam param) {
//        //1.分页参数（PageHelper）
//        Integer page = param.getPage();
//        Integer pageSize = param.getPageSize();
//        PageHelper.startPage(page, pageSize);
//        //2.执行查询
//        List<Emp> list = empMapper.list(name, gender, begin, end);
//        //3.封装返回,total?
//        Page<Emp> p = (Page<Emp>) list;
//        long total = p.getTotal();
//        return new PageResult<Emp>(total,list);
//    }

    @Override
    public PageResult<Emp> page(EmpQueryParam param) {
        //1.分页参数（PageHelper）
        Integer page = param.getPage();
        Integer pageSize = param.getPageSize();
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<Emp> list = empMapper.list(param);
        //3.封装返回,total?
        Page<Emp> p = (Page<Emp>) list;
        long total = p.getTotal();
        return new PageResult<Emp>(total,list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        try {
            //保存员工的基本信息
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);
            //2.保存员工对应的工作经历信息
            List<EmpExpr> emprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(emprList)){
                //遍历集合，为emp.id赋值
                emprList.forEach(empExpr->{
                    empExpr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(emprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null,LocalDateTime.now(),"添加员工信息");
            empLogService.insertLog(empLog);
        }


    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //
        empMapper.deleteByIds(ids);
        //
        empExprMapper.deleteByEmpIds(ids);
    }
}
